package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 * 
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;

    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    @Override
    @Transactional
    public List<TbBusinessTrackRecord> getRecordList(Long id) {
        List<TbBusinessTrackRecord> recordList = tbBusinessTrackRecordMapper.getRecordList(id);
        ArrayList<String> arrayList = new ArrayList<>();
        for (TbBusinessTrackRecord tbBusinessTrackRecord : recordList) {
            String record = String.valueOf(tbBusinessTrackRecord.getKeyItems());
            String[] split = record.split(",");
            for (String s : split) {
                String msg = tbBusinessTrackRecordMapper.getMsg(s);
                arrayList.add(msg);
            }
            tbBusinessTrackRecord.setKeys(arrayList);
        }
        return recordList;
    }

    @Override
    @Transactional
    public int save(BusinessTrackVo businessTrackVo) {
        TbBusiness tbBusiness = new TbBusiness();
        tbBusiness.setClueId(businessTrackVo.getBusinessId());
        tbBusiness.setStatus("2");
        tbBusiness.setName(businessTrackVo.getName());
        tbBusiness.setPhone(businessTrackVo.getPhone());
        tbBusiness.setOccupation(businessTrackVo.getOccupation());
        tbBusiness.setEducation(businessTrackVo.getEducation());
        tbBusiness.setProvinces(businessTrackVo.getProvinces());
        tbBusiness.setCity(businessTrackVo.getCity());
        tbBusiness.setWeixin(businessTrackVo.getWeixin());
        tbBusiness.setAge(businessTrackVo.getAge());
        tbBusiness.setMajor(businessTrackVo.getMajor());
        tbBusiness.setJob(businessTrackVo.getJob());
        tbBusiness.setSalary(businessTrackVo.getSalary());
        tbBusiness.setQq(businessTrackVo.getQq());
        tbBusiness.setSex(businessTrackVo.getSex());
        tbBusiness.setExpectedSalary(businessTrackVo.getExpectedSalary());
        tbBusiness.setRemark(businessTrackVo.getRemark());
        tbBusiness.setSubject(businessTrackVo.getSubject());
        tbBusiness.setReasons(businessTrackVo.getReasons());
        tbBusiness.setPlan(businessTrackVo.getPlan());
        tbBusiness.setPlanTime(businessTrackVo.getPlanTime());
        tbBusiness.setCourseId(businessTrackVo.getCourseId());
        tbBusiness.setOtherIntention(businessTrackVo.getOtherIntention());
        tbBusiness.setChannel("0");
        tbBusiness.setActivityId(businessTrackVo.getActivityId());
        tbBusinessMapper.updateTbBusiness(tbBusiness);

        int result = tbBusinessTrackRecordMapper.save(businessTrackVo);
        return result;
    }
}
